import { Controller, Get, Post, Body, Patch, Param, Delete } from '@nestjs/common';
import { GoodsCategoryService } from './goods-category.service';
import { CreateGoodsCategoryDto } from './dto/create-goods-category.dto';
import { buildTree } from 'src/utils/fun';

@Controller('goods-category')
export class GoodsCategoryController {
  constructor(private readonly goodsCategoryService: GoodsCategoryService) {}

  @Post('save')
  async create(@Body() createGoodsCategoryDto: CreateGoodsCategoryDto) {
    const result = await this.goodsCategoryService.save(createGoodsCategoryDto);
    return { 
      message: result ? '操作成功': '失败'
    }
  }

  @Get('load')
  async findAll() {
    const result = await this.goodsCategoryService.findAll();
    if(result){
      return {
        data: buildTree(result)
      }
    }
  }

  @Get(':id')
  findOne(@Param('id') id: string) {
    return this.goodsCategoryService.findOne(+id);
  }

  @Get('delete/:id')
  async delete(@Param('id') id: string) {
    const result = await this.goodsCategoryService.delete(id);
    return { 
      message: result ? '完全删除成功': '失败'
    }
  }
  @Get('remove/:id')
  async remove(@Param('id') id: string) {
    const result = await this.goodsCategoryService.remove(id);
    return { 
      message: result ? '删除成功': '失败'
    }
  }
}
